package com.firesrc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.collection.SimpleQueue;

import com.firesrc.bean.MailDayAmount;
import com.firesrc.bean.MailHost;
import com.firesrc.dao.MailHostDao;
import com.firesrc.dao.impl.MailHostDaoImpl;
import com.firesrc.service.MailDayAmountService;
import com.firesrc.service.MailHostService;

public class MailHostServiceImpl implements MailHostService {
  private MailHostDao dao;

  public MailHostServiceImpl() {
    dao = new MailHostDaoImpl();
  }

  @Override
  public void add(MailHost mailHost) {
    dao.add(mailHost);

  }

  @Override
  public List<MailHost> getAllList() {
    return dao.getAllList(new MailHost());
  }

  @Override
  public void delete(String id) {
    MailHost mailHost = new MailHost();
    mailHost.setId(id);
    dao.delete(mailHost);

  }

  @Override
  public List<MailHost> getAllCurrectList() {
    MailDayAmountService mailDayAmountService = new MailDayAmountServiceImpl();
    List<MailHost> list = dao.getEnableList();
    List<MailDayAmount> todayList = mailDayAmountService.getTodayList();
    Map<String, Integer> map = new HashMap<String, Integer>();

    for (MailDayAmount mailDayAmount : todayList) {
      map.put(mailDayAmount.getUserName(), mailDayAmount.getAmount());
    }
    for (MailHost mailHost : list) {
      mailDayAmountService.put(mailHost);
    }
    List<MailHost> allCurrentList = dao.getAllCurrentList();
    for (MailHost mailHost : allCurrentList) {
      mailHost.setDayAlreadySendCount(map.get(mailHost.getMail()));
    }
    return allCurrentList;
  }

  @Override
  public List<String> getAllCurrentKindList() {
    return dao.getAllCurrentKindList();
  }

  @Override
  public void enable(String id) {
    dao.enable(id);

  }

  @Override
  public void disable(String id) {
    dao.disable(id);

  }

  @Override
  public void update(MailHost mailHost) {
    dao.update(mailHost);
  }

  @Override
  public void resetSeq(int count) {
    List<String> kindList = getAllCurrentKindList();
    Map<String, SimpleQueue<MailHost>> map = new HashMap<String, SimpleQueue<MailHost>>();
    for (String kind : kindList) {
      map.put(kind, new SimpleQueue<MailHost>());
    }

    List<MailHost> mailHostList = getAllCurrectList();
    for (MailHost mailHost : mailHostList) {
      SimpleQueue<MailHost> simpleQueue = map.get(mailHost.getKind());
      if (simpleQueue != null) {
        simpleQueue.put(mailHost);
      }
    }

    int seq = 0;
    for (int i = 0; i < count; i++) {
      for (String kind : kindList) {
        seq++;
        MailHost mailHost = map.get(kind).get();
        if (mailHost != null) {
          mailHost.setSeq(seq);
          update(mailHost);
        }

      }
    }
    System.out.println("MailHostServiceTest.testDuiLie()" + map);
  }

  @Override
  public Map<String, Integer> getKindCountMap(String state) {
    return dao.getKindCountMap(state);

  }

  @Override
  public List<MailHost> getListByType(String type) {
    return dao.getListByType(type);
  }

  public MailHost getMailHostByMail(String mail) {
    Map<String, MailHost> map = new HashMap<String, MailHost>();
    MailHost qqMailHost = new MailHost();
    qqMailHost.setHostName("smtp.qq.com");
    qqMailHost.setKind("qq");
    map.put("qq.com", qqMailHost);
    return map.get("qq.com");
  }

  @Override
  public MailHost getMailHostById(String id) {
    return dao.getById(id);

  }

  @Override
  public MailHost save(MailHost mailHost) {
    dao.save(mailHost);
    return getMailHostById(mailHost.getId());
  }

  @Override
  public int disableAll() {
    return dao.disableAll();

  }

  @Override
  public int enableAll() {
    // TODO Auto-generated method stub
    return dao.enableAll();
  }
}
